package base;

import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 * @author qsj
 * 
 */

public class BaseDispatchAction extends DispatchAction {
	protected Class[] types = { Power.class };

	protected Method getMethod(String name) throws NoSuchMethodException {

		synchronized (methods) {
			Method method = (Method) methods.get(name);
			if (method == null) {
				method = clazz.getMethod(name, types);
				methods.put(name, method);
			}
			return (method);
		}

	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// Get the parameter. This could be overridden in subclasses.
		response.setContentType("text/html;charset=UTF-8");
		String parameter = mapping.getParameter();

		// Get the method's name. This could be overridden in subclasses.
		String name = request.getParameter(parameter);
		// Prevent recursive calls
		if ("execute".equals(name) || "perform".equals(name)) {
			String message = messages.getMessage("dispatch.recursive", mapping
					.getPath());

			// log.error(message);
			throw new ServletException(message);
		}

		// Invoke the named method, and return the result
		return dispatchMethod(mapping, form, request, response, name);

	}

	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {

		// Make sure we have a valid method name to call.
		// This may be null if the user hacks the query string.
		if (name == null) {
			return this.unspecified(mapping, form, request, response);
		}
		// Identify the method object to be dispatched to
		Method method = null;
		try {
			method = getMethod(name);

		} catch (NoSuchMethodException e) {
			messages.getMessage("dispatch.method", mapping.getPath(), name);
			// log.error(message, e);

			String userMsg = messages.getMessage("dispatch.method.user",
					mapping.getPath());
			throw new NoSuchMethodException(userMsg);
		}

		ActionForward forward = null;
		Sql sql = new Sql();
		try {
			// Object args[] = { mapping, form, request, response };

			Power power = new Power(mapping, form, request, response, sql);
			Object args[] = { power };
			forward = (ActionForward) method.invoke(this, args);

		} catch (Exception e) {
			response.getWriter().println(
					"<%@ page contentType=\"text/html; charset=UTF-8\" %>");
			response.getWriter().println("系统错误" + "<!--");
			e.printStackTrace(response.getWriter());
			response.getWriter().println("-->");

		} finally {
			sql.close();
		}

		// Return the returned ActionForward instance
		return (forward);
	}

	protected void render(HttpServletResponse response, String text,
			String contentType) {
		try {
			response.setContentType(contentType);
			response.getWriter().write(text);
		} catch (Exception e) {
			// log.error(e.getMessage(), e);
		}
	}

	protected void renderText(HttpServletResponse response, String text) {
		render(response, text, "text/plain;charset=UTF-8");
	}

	protected void renderHtml(HttpServletResponse response, String text) {
		render(response, text, "text/html;charset=UTF-8");
	}

	protected void renderXML(HttpServletResponse response, String text) {
		render(response, text, "text/xml;charset=UTF-8");
	}

}
